/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: LENOVO
 * Date: 2024-08-07
 * Time: 23:38
 */
public class MyLinkedList implements IOperations{
    static class Node{
        int val;
        Node prev;
        Node next;
        public Node(int val) {
            this.val = val;
        }
    }
    static Node head = null;
    static Node last = null;
    @Override
    public void display() {
        Node cur = head;
        while(cur != null){
            System.out.print(cur.val + " ");
            cur = cur.next;
        }
        System.out.println();
    }

    @Override
    public int size() {
        Node cur = head;
        int count = 0;
        while(cur != null){
            count++;
            cur = cur.next;
        }
        return count;
    }

    @Override
    public boolean contains(int key) {
        Node cur = head;
        while(cur != null){
            if(cur.val == key){
                return true;
            }
            cur = cur.next;
        }
        return false;
    }

    @Override
    public void addFirst(int val) {
        Node nodeAdd = new Node(val);
        if(head == null){
            head = nodeAdd;
            last = nodeAdd;
        }else{
            nodeAdd.next = head;
            head.prev = nodeAdd;
            head = nodeAdd;
        }
    }
    @Override
    public void addLast(int val) {
        Node nodeAdd = new Node(val);
        if(head == null){
            head = nodeAdd;
            last = nodeAdd;
        }else{
            last.next = nodeAdd;
            nodeAdd.prev =last;
            last = nodeAdd;
        }
    }

    @Override
    public void addIndex(int index,int val) {
        int len = size();
        Node addNode = new Node(val);
        if(index < 0 || index > len){
            System.out.println("位置错误");
            return;
        }
        if(index == 0){
            addFirst(val);
            return;
        }
        if(index == len){
            addLast(val);
            return;
        }
        Node cur = head;
        while(index != 0){
            cur = cur.next;
            index--;
        }
        cur.prev.next = addNode;
        addNode.next = cur;
        addNode.prev =cur.prev;
        cur.prev = addNode;

        return;
    }

    @Override
    public void remove(int key) {
        if(head == null){
            System.out.println("空链表不支持删除");
            return;
        }
        Node cur = head;
        while(cur != null){
            if(cur.val == key){
                if(cur == head){
                    head = cur.next;
                    if(cur.next != null) {//防止只有只有一个节点
                        cur.next.prev = null;
                    }
                    return;
                }else if(cur == last){
                    cur.prev.next = null;
                    last = cur.prev;
                    return;
                }else{
                    cur.next.prev = cur.prev;
                    cur.prev.next = cur.next;
                    return;
                }
            }
            cur = cur.next;
        }
        return;
    }

    @Override
    public void removeAllKey(int key) {
        if(head == null){
            System.out.println("空链表不支持删除");
            return;
        }
        Node cur = head;
        while(cur != null){
            if(cur.val == key){
                if(cur == head){
                    head = cur.next;
                    if(cur.next != null) {//防止只有只有一个节点
                        cur.next.prev = null;
                    }
                }else if(cur == last){
                    cur.prev.next = null;
                    last = cur.prev;
                }else{
                    cur.next.prev = cur.prev;
                    cur.prev.next = cur.next;
                }
            }
            cur = cur.next;
        }
        return;
    }

    @Override
    public void clear() {
        if(head == null){
            System.out.println("空链表不支持清除");
            return;
        }
        Node cur = head;
        Node curN = head;
        head = null;
        last = null;
        while(cur != null){
            curN= cur.next;
            cur.prev = null;
            cur.next = null;
            cur = curN;
        }
    }
}
